# -*- coding: UTF-8 -*-

from __future__ import absolute_import

from PyQt4.QtCore import QObject
from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import pyqtSignal

from ninja_ide.gui.main_panel import itab_item


###############################################################################
# Services to plugins
###############################################################################


class EditorService(QObject):
    editorKeyPressEvent = pyqtSignal("QEvent")

    def __init__(self, mainContainer):
        QObject.__init__(self)
        self.__main = mainContainer
        self.connect(self.__main, SIGNAL("editorKeyPressEvent(QEvent)"),
            self._keyPressEvent)

    def add_menu(self, menu, lang="py"):
        itab_item.ITabItem.add_extra_menu(menu, lang=lang)

    def get_editor(self):
        return self.__main.get_actual_editor()

    def get_editor_path(self):
        return self.__main.get_actual_editor().ID

    def add_editor(self, fileName=""):
        return self.__main.add_editor(fileName=fileName)

    def get_text(self):
        return self.__main.get_actual_editor().get_text()

    def insert_text(self, text):
        self.__main.get_actual_editor().textCursor().insertText(text)

    def jump_to_line(self, lineno):
        self.__main.get_actual_editor().jump_to_line(lineno=lineno)

    def get_lines_count(self):
        editor = self.__main.get_actual_editor()
        return editor.textCursor().document().lineCount()

    def _keyPressEvent(self, event):
        self.editorKeyPressEvent.emit(event)


class ToolbarService(QObject):

    def __init__(self, toolbar):
        QObject.__init__(self)
        self.__toolbar = toolbar

    def add_action(self, action):
        self.__toolbar.addAction(action)


class MenuAppService(QObject):

    def __init__(self, plugins_menu):
        QObject.__init__(self)
        self.__plugins_menu = plugins_menu

    def add_menu(self, menu):
        self.__plugins_menu.addMenu(menu)

    def add_action(self, action):
        self.__plugins_menu.addAction(action)


class ProjectTypeService(QObject):
    def __init__(self):
        QObject.__init__(self)

    def add_project_type_manager(self, project_type, prpject_type_manager):
        #TODO: Meter esto donde corresponda no en DIC
        #DIC[project_type] = project_type_manager
        pass
